package com.bang.daybreak.security.config.handler;

import com.bang.daybreak.common.util.JacksonUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * <p>
 *     登出成功
 * </p>
 *
 * @version 1.0
 * @author: wdj
 * @date: 2019/8/11 0011 下午 17:53
 */
@Component
public class IndentityLogoutSuccessHandler implements LogoutSuccessHandler  {
    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        map.put("timestamp", System.currentTimeMillis());
        map.put("httpCode", HttpStatus.OK.value());
        map.put("error", "USER_LOGOUT_SUCCESS");
        map.put("msg", "您已登出成功!");
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        httpServletResponse.setStatus(HttpStatus.OK.value());
        httpServletResponse.getWriter().write(JacksonUtils.toJson(map));
    }
}
